import sys
import math

f = open("/<WORKSPACE>/tp1/experimentos/msg_big")
probab=open('datosEntropia.txt','w')
archi=open('datosEntropia.txt','a')
agregados = dict()
registerCount = 0
maxRegisterCount = 15000;

def addToDict(key):
        try:
            count = agregados[key]
            count = count +1
            agregados[key] = count
        except KeyError:
            agregados[key] = 1
            

def count():
        entropy = 0
        for key in agregados:
                probab.write(" IP = ")
                probab.write(key)
                probab.write(" , ")
                probab.write(" # apariciones = ")
                probab.write(str(agregados[key]))
                probab.write(" , ")
                probab.write("probabilidad = ")
                prob = float(agregados[key])/maxRegisterCount
                probab.write(str(prob))
                probab.write("\n")  
                
                entropy += prob * math.log(1/prob, 2)
        
        probab.write("Entropia por " + str(maxRegisterCount) + " simbolos = ")        
        probab.write(str(entropy))     
    
                
def setGratuitousNodeAddress():
    gratuitous_last_index = l.find("(Request)")
    from_addr = to_addr = l[gratuitous_first_index + 19 : gratuitous_last_index - 1]
    
    
def setNodeAddress():
    from_addr = l[tell_index + 5 : len(l) - 1]
    who_has_index = l.find("Who has ")
    q_mark_index = l.find("? ")
    to_addr = l[who_has_index + 8 : q_mark_index]
    
for l in f:
    registerCount += 1    
    if (registerCount == maxRegisterCount) :
        break;
    
    gratuitous_first_index = l.find("Gratuitous ARP for ")    
    if gratuitous_first_index != -1 :
        gratuitous_last_index = l.find("(Request)")
        from_addr = to_addr = l[gratuitous_first_index + 19 : gratuitous_last_index - 1]                                 
            
    else :
        tell_index = l.find("Tell ")        
        if tell_index != -1 :
            from_addr = l[tell_index + 5 : len(l) - 1]
            who_has_index = l.find("Who has ")
            q_mark_index = l.find("? ")
            to_addr = l[who_has_index + 8 : q_mark_index]                                                                     
        else :
            continue 
    
    addToDict(from_addr)
    addToDict(to_addr)
	

count()
f.close()
probab.close()
